package com.ruoyi.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.common.utils.uuid.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;

public class ElongUtils {

    private static Logger hotelSuccess = LoggerFactory.getLogger("hotel_success");


    // 艺龙测试地址
    private static String elongTestUrl = "api-test.elong.com";

    // 艺龙生产地址
    private static String elongProdUrl = "api.elong.com";

    // user
    private static String user = "cc23aeae22cf4eeb96be7acd20c991f9";

    // appKey
    private static String appKey = "3570624dc3b539aa58babfd773c6a5c3";

    // seretKey
    private static String secretKey = "28ea76bbf827f8431191b0b80ec85892";

    /**
     * 获取城市信息
     */
    public static String getCity(int pageNum, int pageSize) {
        try {
            // 先封装data
            JSONObject data = new JSONObject();
            data.put("Version", "1.50");
            data.put("Local", "zh_CN");
            JSONObject requestParam = new JSONObject();
            requestParam.put("CountryType", 1);
            requestParam.put("IsNeedLocation", false);
            requestParam.put("PageSize", pageSize);
            requestParam.put("PageIndex", pageNum);

            data.put("Request", requestParam);
            // 按要求进行URLEncode处理
            String encodedString = URLEncoder.encode(data.toJSONString(), "UTF-8");

            // 时间戳
            long epoch = System.currentTimeMillis() / 1000;

            // 获取签名md5(timestamp + md5(data + appkey) + secretKey)
            String sign = Md5Utils.hash(epoch + Md5Utils.hash(data.toJSONString() + appKey) + secretKey);

            // 拼接访问接口的url
            String url = "https://" + elongProdUrl + "/rest?format=json&method=hotel.static.city";
            url += "&user=" + user + "&timestamp=" + epoch + "&signature=" + sign;
            url += "&data=" + encodedString;
            String s = HttpUtils.sendGet(url);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取酒店列表
     */
    public static JSONArray getHotelList(String cityId, String hotelName) {
        try {
            int dataNum = 0;
            JSONArray jsonArray1 = new JSONArray();
            int pageNum = 1;
            do {
                int pageSize = 5000;
                // 先封装data
                JSONObject data = new JSONObject();
                data.put("Version", "1.50");
                data.put("Local", "zh_CN");
                JSONObject requestParam = new JSONObject();
                requestParam.put("CityId", cityId);
                requestParam.put("PageSize", pageSize);
                requestParam.put("PageIndex", pageNum);

                data.put("Request", requestParam);
                // 按要求进行URLEncode处理
                String encodedString = URLEncoder.encode(data.toJSONString(), "UTF-8");

                // 时间戳
                long epoch = System.currentTimeMillis() / 1000;

                // 获取签名md5(timestamp + md5(data + appkey) + secretKey)
                String sign = Md5Utils.hash(epoch + Md5Utils.hash(data.toJSONString() + appKey) + secretKey);

                // 拼接访问接口的url
                String url = "https://" + elongProdUrl + "/rest?format=json&method=hotel.static.list";
                url += "&user=" + user + "&timestamp=" + epoch + "&signature=" + sign;
                url += "&data=" + encodedString;
                String s = HttpUtils.sendGet(url);

                JSONObject jsonObject = JSONObject.parseObject(s);

                JSONArray jsonArray = jsonObject.getJSONObject("Result").getJSONArray("Hotels");
                dataNum = jsonArray.size();

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    if(jsonObject1.getInteger("HotelStatus") > 0){
                        continue;
                    }
                    if(jsonObject1.getString("HotelName").indexOf(hotelName) == -1){
                        continue;
                    }
                    jsonArray1.add(jsonObject1);
                }
                pageNum++;
            } while (dataNum > 0);

            return jsonArray1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取酒店详情
     *
     * @return
     */
    public static String getHotelInfo(String hotelId) {
        try {
            // 先封装data
            JSONObject data = new JSONObject();
            data.put("Version", "1.50");
            data.put("Local", "zh_CN");
            JSONObject requestParam = new JSONObject();
            requestParam.put("HotelId", hotelId);
            requestParam.put("Options", "1,3");

            data.put("Request", requestParam);
            // 按要求进行URLEncode处理
            String encodedString = URLEncoder.encode(data.toJSONString(), "UTF-8");

            // 时间戳
            long epoch = System.currentTimeMillis() / 1000;

            // 获取签名md5(timestamp + md5(data + appkey) + secretKey)
            String sign = Md5Utils.hash(epoch + Md5Utils.hash(data.toJSONString() + appKey) + secretKey);

            // 拼接访问接口的url
            String url = "https://" + elongProdUrl + "/rest?format=json&method=hotel.static.info";
            url += "&user=" + user + "&timestamp=" + epoch + "&signature=" + sign;
            url += "&data=" + encodedString;
            String s = HttpUtils.sendGet(url);
            return s;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String apiKey = "testapikey";

    // public static String userName = "lpecy";

    // public static String password = "autotest";

    public static String userName = "ahyy";

    public static String password = "ahyy";


    public static String login() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apiKey", apiKey);

        JSONObject userInfo = new JSONObject();
        userInfo.put("password", password);
        userInfo.put("userName", userName);

        jsonObject.put("body", userInfo);
        jsonObject.put("encrypted", 0);
        jsonObject.put("guid", UUID.randomUUID().toString());
        jsonObject.put("method", "login");
        jsonObject.put("timestamp", System.currentTimeMillis());
        jsonObject.put("version", "v2");

        // 上线这里解封
        String res = HttpUtils.sendPostJson("https://opendc.elong.com/gate/v2/rest?aid=1073742141", JSONObject.toJSONString(jsonObject));
        JSONObject jsonObject1 = JSONObject.parseObject(res);
        String loginToken = jsonObject1.getJSONObject("body").getString("loginToken");
        return loginToken;

        /*
        return "{\n" +
                "  \"body\": {\n" +
                "    \"expiredTime\": \"1472717684677\",\n" +
                "    \"isv\": \"5399252615759774\",\n" +
                "    \"key\": \"gn4Vg5Qlag71fWvc\",\n" +
                "    \"loginToken\": \"6d394b1d-351d-4d8e-ac17-2530f78b2296\"\n" +
                "  },\n" +
                "  \"retcode\": 0,\n" +
                "  \"retdesc\": \"SUCCESS\",\n" +
                "  \"timestamp\": 1472715658960\n" +
                "}";
        */
    }

    public static void main(String[] args) {
        String hotelInfo = getHotelInfo("16242039");
        System.out.println(hotelInfo);
    }
}